package com.comp5900.testing;


import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;

import com.comp5900.impl.huffman.*;
import com.comp5900.impl.lcs.LongestCommonSubsequenceFinder;
import com.comp5900.util.HuffmanUtility;


public class CopyDetectionTest {

	@Test
	public void testHuffmanEncoding(){
		String teststring = "jaacccoooobbbbb";
		String expectedvalue = "00101101";

		int[] charfreqs = new int[256];
		ArrayList<SymbolFrequencyPair<Byte>> map = new ArrayList<SymbolFrequencyPair<Byte>>();

		//cache the frequencies of the symbols
		for ( char c : teststring.toCharArray() )
			charfreqs[c]++;
 
		//convert to object arraylist
		for( int i=0; i<charfreqs.length; i++)
			if( charfreqs[i] > 0)
				map.add( new SymbolFrequencyPair<Byte>(Byte.valueOf((byte)i), charfreqs[i]) );
			
		//build tree
		HuffmanTree tree = HuffmanEncoder.buildEncodingTree(map);
 
		//flatten the code
		String traversal = HuffmanUtility.getInorderTraversalOfEncodingTree(tree);

		assertEquals(expectedvalue, traversal);
	}

	@Test
	public void testLongestCommonSubsequence(){
		String haystack = "cats and dogs";
		String needle = "zczzozgzz";
		String expected = "cog";
		String result = "";

		//convert the strings to Character arrays
		ArrayList<Character> a = new ArrayList<Character>(), b = new ArrayList<Character>();

		for( char c : haystack.toCharArray() )
			a.add(c);

		for( char c : needle.toCharArray() )
			b.add(c);

		//Find the longest common subsequence
		List<?> l = LongestCommonSubsequenceFinder.computeLongestCommonSubsequence( a.toArray(), b.toArray() );
	

		for(Object o : l){
			result += o;
		}

		assertEquals(expected, result);
	}
}
